package com.seven.good.scheduler;

import com.seven.good.dao.GoodMapper;
import com.seven.good.pojo.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class AuctionScheduler {

    @Autowired
    GoodMapper goodMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * @Description：开启定时任务，选取竞拍时间段的商品，以String存入redis，并修改商品拍卖状态
     * @Author: ytj
     * @CreateDate:  19/10/17
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void listNowAuctionGoods(){
        List<Good> list = goodMapper.listGoodsIsAuction();
        for (Good good : list) {
            // 设置商品状态
            good.setGoodState(2);
           /* // 存入map，key值为 商品类型ID.商品ID
            map.put(good.getGid().toString(),good);*/
            // 更新数据库商品状态
            goodMapper.updateGoodStateById(good.getGid());
            // 设置过期时间
            Long time = (good.getEndTime().getTime()-new Date().getTime())/1000;

            redisTemplate.opsForValue().set(good.getGid().toString(),good,time,TimeUnit.SECONDS);
        }

    }
}
